/**
 * Created with IntelliJ IDEA.
 * User: guoshunw
 * Date: 13-11-5
 * Time: 下午12:27
 * To change this template use File | Settings | File Templates.
 */

//@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.6')

class Parameter<String, String> {

    Parameter(java.lang.String type, java.lang.String value) {
        this.type = type
        this.value = value
    }

    String type
    String value
}

import groovyx.net.http.HTTPBuilder
import net.sf.json.JSONObject
import org.apache.commons.io.HexDump

import static groovyx.net.http.ContentType.JSON

/***
 * Call IME Phone remote methods
 * @param method method to call
 * @param parameters method parameters
 * @param module module of ApplicationManager
 * @param host host of the remote Phone
 * @param callback of response closure
 */

void CallRemoteMethod(String method = "getIMECandidates",
                      Collection<Parameter> parameters = [new Parameter("QString", "session"), new Parameter("QString", "wang")],
                      String module = "IMEManager", String host = "localhost:8088", Closure responseClosure = { resp, json ->
    String candidatesStr = json['value']['candidates']
    println new String(candidatesStr.getBytes("ISO8859-1"), "utf-8")

}) {
    if (null == method || null == module) return

    String url = "http://${host}"
    String path = "/127.0.0.1:1665/services/${module}/${method}"

    Map<String, String> queryData = [:]
    parameters.eachWithIndex { Parameter<String, String> parameter, int i ->
        queryData["json${i}"] = new JSONObject('type': parameter.type, 'value': parameter.value).toString()
    }
    HTTPBuilder http = new HTTPBuilder(url)
    http.get([path: path, contentType: JSON, query: queryData], responseClosure)
}
//String host ="localhost:8088"
host ="135.251.222.210"
host ="10.10.15.122"

//CallRemoteMethod("startIMESession", [new Parameter("int", "0")], "IMEManager", host){resp,json->  println(json)}
CallRemoteMethod("getIMECandidates", [new Parameter("QString", "session"), new Parameter("QString", "wang")], "IMEManager", host)
//        {resp,json->println(json)}




// javascript for(var i=0; i<a.length; ++i){var chCode=a.charCodeAt(i); console.log(Number(chCode).toString(16));}
//String a = "吴a国b"
//byte[] bytes = a.getBytes("UTF-8")
//HexDump.dump(bytes, 0, System.out,0)
//String errStr = new String(bytes, "ISO8859-1")
//println errStr
//byte[] rBytes = errStr.getBytes("ISO8859-1")
//HexDump.dump(rBytes, 0 , System.out, 0)
//String correctString = new String(rBytes, "UTF-8")
//println correctString
